package com.shop.web.servlet;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.shop.dao.UserDao;
import com.shop.domain.User;

/**
 */
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		//先判断验证码
		String code = request.getParameter("checkcode");
		String session_code = (String) request.getSession().getAttribute("session_code");
		request.getSession().removeAttribute("session_code");
//		if (code == null || !code.equals(session_code)) {
//			request.setAttribute("msg", "验证码错误！");
//			request.getRequestDispatcher("/regist.jsp").forward(request, response);
//		}else {
			User user = new User();
			try {
				BeanUtils.populate(user, request.getParameterMap());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
		//	UserService service = new UserService();
			//service.regist(user);
			response.getWriter().println("注册成功！激活邮件已发送到您的邮箱，请于两小时内激活");
			UserDao dao = new UserDao();
			dao.insert(user);
		}
	}


